package net.zjitc.dao.impl;

import net.zjitc.dao.UserDao;
import net.zjitc.domain.User;
import net.zjitc.utils.JdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {
    JdbcTemplate template = new JdbcTemplate(JdbcUtils.getDataSource());
    @Override
    public List<User> findALL() {
        String sql = "select * from user";
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class));
        return list;
    }

    //重写
    @Override
    public User findByUsernameAndPassword(String username, String password) {
        String sql = "select * from user where username = ? and password = ?";
        //queryForObject：流程是结果为0或者>1的时候，都抛出异常
        //自己处理
        //User user = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username, password);
        List<User> list = template.query(sql, new BeanPropertyRowMapper<>(User.class), username, password);
        if (list.size()==0){
            return null;
        }
        if (list.size()==1){
            return list.get(0);
        }
        //数据库中会存在两个记录，不会的，因为用户注册的时候 ，会判断是否已经有此用户，如果有不允许注册，故不存在两条相同记录

        return null;
    }

    @Override
    public int add(User user) {

        return 0;
    }

    @Override
    public int update(User user) {
        return 0;
    }

    @Override
    public int delete(int id) {
        return 0;
    }
}
